/*
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License. 
*/

package com.google.enterprise.gsa.crf;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Collection;
import java.util.Hashtable;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

import java.util.logging.Level;
import java.util.logging.Logger;




/**
 * ServiceManager manages the hash table of services. It loads the service
 * descriptions from the XML configuration file "aaservices.xml"
 */
public class

ServiceManager {
    private Hashtable<String, Service> services = null;
    private static Logger log = null;

    public ServiceManager() {
        log = Logger.getLogger(ServiceManager.class.getName());
    }

    @SuppressWarnings("unchecked")
	public void loadServices() throws MalformedURLException, 
                                      ConfigurationException, 
                                      InstantiationException, 
                                      IllegalAccessException, 
                                      ClassNotFoundException {
        XMLConfiguration config = null;
        services = new Hashtable<String, Service>();
        config = new XMLConfiguration("aasrservices.xml");
        log.fine("aasrservices.xml file loaded");
        Object prop = config.getProperty("service[@name]");

        if (prop instanceof Collection) {
            int colSize = ((Collection<Object>)prop).size();
            for (int i = 0; i < colSize; i++) {
                Service s;
                String className = 
                    config.getProperty("service(" + i + ")[@class]").toString();
                    
                s = (Service)Class.forName(className).newInstance();
                
                String serviceName = 
                    config.getProperty("service(" + i + ")[@name]").toString();
                s.setName(serviceName);
                s.setUser(config.getProperty("service(" + i + 
                                             ").login.username").toString());
                s.setPasswd(config.getProperty("service(" + i + 
                                               ").login.password").toString());
                s.setDateformat(config.getProperty("service(" + i + 
                                                   ").dateformat").toString());
                URL url = null;
                url = 
                        new URL(config.getProperty("service(" + i + ").url").toString());

                s.setUrl(url);
                s.setAdditional(config.getProperty("service(" + i + 
                                                   ").additional").toString());       
                s.init();
                services.put(serviceName, s);
                log.log(Level.FINE, "Service "  + serviceName + " loaded");
            }
        } else {
            Service s;
            String className = 
                config.getProperty("service@[class]").toString();
                
            s = (Service)Class.forName(className).newInstance();
            
            String serviceName = config.getProperty("service@[name]").toString();
            s.setName(serviceName);
            s.setUser(config.getProperty("service.login.username").toString());
            s.setPasswd(config.getProperty("service.login.password").toString());
            s.setDateformat(config.getProperty("service.dateformat").toString());
            URL url = null;
            url = 
                    new URL(config.getProperty("service.url").toString());
            s.setUrl(url);
            s.setAdditional(config.getProperty("service.additional").toString());
            s.init();
            services.put(serviceName, s);
            log.log(Level.FINE, "Service "  + serviceName + " loaded");
        }
    }

    public Service getService(String s) {
        Service serv = null;
        serv = (Service)(services.get(s));
        return serv;
    }
}
